﻿ 
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Reflection;

namespace HBase
{
    /**
   *┌────────────────────────────────────────────────┐
   *│　描    述：Processer                                                    
   *│　作    者：sgj                                              
   *│　版    本：1.0                                              
   *│　创建时间：2021/9/8 15:37:45                        
   *└────────────────────────────────────────────────┘
   **/
    public class Processer {
        public string HttpUrl;
        public Dictionary<String ,object> param;

        public string httpMethod = "";
        public ControlName controlName;
        internal BaseResultAbs Process(Stream _inputStream ,NetworkStream networkStream) {
            BaseResult res = new BaseResult();
            var request = Utils.ReadLine(_inputStream);
            MyConsole.addMessage(request);
            var tokens = request.Split(' ');
            if( tokens.Length != 3 ) {
                res.code = 501;
                res.data = "invalid http request line";
                return res;
            }
            httpMethod = tokens[0].ToUpper();
            HttpUrl = tokens[1];
            if( httpMethod == "OPTIONS" ) {
                res.code = 0;
                res.data = "";
                return res;
            }

            String[] uri = HttpUrl.Split('?');
            HttpParam newparam = null;
            try {
                newparam = HttpApiFacade.getAction(uri[0]);
                if( newparam == null ) {
                    res.code = 502;
                    res.data = " path error" + HttpUrl;
                    return res;
                }
            } catch( Exception e ) {
                LoggerCommon.error(HttpUrl ,e);
                res.code = 504;
                res.data = e.Message + "aciton 异常";
                return res;
            }
            try {


                Hashtable ht = Utils.ParseRequest(_inputStream);

                if( ht.ContainsKey("token") ) {
                    String token = ht["token"].ToString();

                    if( newparam.meth.authorization ) {
                        if( CookieHelper.isTockenTimeOut(token) ) {
                            return BaseResult.Error("操作超时" ,20000);
                        }
                    }
                    CookieHelper.addtoken(token);
                }


                return doAction(ht ,_inputStream ,newparam ,uri ,httpMethod);

            } catch( Exception e ) {
                LoggerCommon.error(HttpUrl ,e);
                res.code = 503;
                res.data = e.Message;
                return res;
            }
        }
        BaseResultAbs doAction(Hashtable ht ,Stream _inputStream ,HttpParam newparam ,String[] uri ,string httpMethod) {
            param = newparam.mc;
            if( uri.Length > 1 ) {
                Utils.getData(uri[1] ,param);
            }

            HttpMethod method = newparam.meth;
            if( httpMethod.Equals("POST") ) {
                String data = Utils.PostRequestExec(ht ,_inputStream);
                ParameterInfo[] info = method.parameterInfos;
                ParameterInfo bodyInfo = method.body;
                if( bodyInfo != null ) {
                    Type bBodyType = bodyInfo.ParameterType;
                    object body = data;
                    if (!bBodyType.IsAssignableTo(typeof (String))) {
                        body = JsonConvert.DeserializeObject(data, bBodyType);
                    }
                     
                    param.Add(bodyInfo.Name.ToString() ,body);
                }
            }

            controlName = method.controlName;
            var v = newparam.doAction();
            if( v is BaseResultAbs ) {
                return ( BaseResultAbs ) v;
            } else {
                return BaseResult.Ok(v);
            }
        }
    }
}
